<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
cfc('core')->checklogin();
$setting=uni_setting('member_setting');
if(!$setting['sign']['open']){
	exi('系统不支持签到！','','goback');
}
if($_RQ['op']=='main'){
	//签到页面
	//需要生成的数据：最近12个月的月份选择数据、当前月份的签到数据
	$year=date('y');
	$month=date('m');
	$today=date('ymd');
	$months=array();
	//$months[]=array('value'=>$year.$month,'title'=>$year.'年'.$month.'月');
	for($i=0;$i<12;$i++){
		if($i<$month){
			//$temp=array('month'=>$month-$i,'year'=>$year);
			$m=$month-$i;
			if($m<10){
				$m='0'.$m;
			}
			$months[]=array('value'=>'20'.$year.$m,'title'=>$year.'年'.$m.'月');
		}else{
			$y='20'.($year-1);
			$m=($month+12-$i);
			if($m<10){
				$m='0'.$m;
			}
			$months[]=array('value'=>$y.$m,'title'=>$y.'年'.$m.'月');
		}
	}
	$signs=pdo_getall('core_qiandao',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'date between'=>array($year.$month.'01',$year.$month.'31')),array('date'),'date');
	$monthdata=array();
	if(in_array($month,array(1,3,5,7,8,10,12))){
		$allday=31;
	}elseif(in_array($month,array(4,6,9,11))){
		$allday=30;
	}else{
		if($year%4){
			$allday=28;
		}else{
			$allday=29;
		}
	}
	for($i=1;$i<=$allday;$i++){
		$time=mktime(0,0,0,$month,$i,$year);
		$temp=date('W.N',$time);
		$temp=explode('.',$temp);
		if(!$monthdata[$temp['0']]){
			$monthdata[$temp['0']]=array();
		}
		$date=array('value'=>date('ymd',$time),'title'=>$i);
		if($signs[$date['value']]){
			$date['signed']=1;
		}
		$monthdata[$temp['0']][$temp['1']]=$date;
	}
	//去除键名
	foreach($monthdata as $row){
		$return['monthdata'][]=$row;
	}
	//$return['monthdata']=$monthdata;
	$return['month_choosed']='20'.$year.$month;
	$return['months']=$months;
	$return['today']=$today;
	//获取签到设置
	$setting=uni_setting('member_setting');
	$return['setting']=$setting['sign'];
	//获取会员信息
	$return['member']=model('core/member')->detail($_SESSION['uid'],array('avatar','nickname','credit1'));
	
	//获取连续签到天数、总签到天数、今天是否签到
	$return['alldays']=pdo_count('core_qiandao',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	if(pdo_getcolumn('core_qiandao',array('mid'=>$_SESSION['uid'],'date'=>$today,'uniacid'=>$_SESSION['uniacid']),'id')){
		$return['signed']=1;
	}
	$return['days']=pdo_getcolumn('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'sign_days');
	$return['weeks']=array(1,2,3,4,5,6,7);
	//获取签到奖品信息
	$rewards=pdo_getall('core_reward',array('uniacid'=>$_SESSION['uniacid']));
	$return['reward_lianxu']=$return['reward_leiji']=array();
	if($rewards){
		foreach($rewards as $item){
			if($item['type']==1){
				$return['reward_lianxu'][]=$item;
			}else{
				$return['reward_leiji'][]=$item;
			}
		}
	}
	
	//$cs=var_export($return,true);file_put_contents('cs.txt',$cs);
	exi($return);
}
if($_RQ['op']=='getmonth'){
	//获取指定月份数据并返回
	$post=get_postdata();
	$data=intval($post['month']);
	if(!$data){
		exi('参数错误！','error');
	}
	$data=substr($data,2);
	$signs=pdo_getall('core_qiandao',array('mid'=>$_SESSION['uid'],'date between'=>array($data.'01',$data.'31'),'uniacid'=>$_SESSION['uniacid']),array('date'),'date');
	$monthdata=array();
	$year=substr($data,0,2);
	$month=substr($data,2,2);
	$allday=date('t',mktime(0,0,0,$month,1,$year));
	for($i=1;$i<=$allday;$i++){
		$time=mktime(0,0,0,$month,$i,$year);
		$temp=date('W.N',$time);
		$temp=explode('.',$temp);
		if(!$monthdata[$temp['0']]){
			$monthdata[$temp['0']]=array();
		}
		$date=array('value'=>date('ymd',$time),'title'=>$i);
		if($signs[$date['value']]){
			$date['signed']=1;
		}
		$monthdata[$temp['0']][$temp['1']]=$date;
	}
	exi($monthdata);
}
if($_RQ['op']=='sign'){
	//点击签到
	$date=date('ymd');
	$check=pdo_getcolumn('core_qiandao',array('mid'=>$_SESSION['uid'],'date'=>$date,'uniacid'=>$_SESSION['uniacid']),'id');
	if($check){
		exi('已经签到，无需重复签到！','error');
	}
	//插入签到数据
	$insert=array(
		'mid'=>$_SESSION['uid'],
		'date'=>$date,
		'uniacid'=>$_SESSION['uniacid'],
		'createtime'=>TIMESTAMP
	);
	pdo_insert('core_qiandao',$insert);
	//统计连续签到，先判断昨天是否签到，如果未签到则重新开始统计。否则添加连续签到次数
	$date=date('ymd',strtotime('-1 day'));
	$check=pdo_getcolumn('core_qiandao',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'date'=>$date),'id');
	if($check){
		pdo_update('core_members',array('sign_days +='=>1),array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	}else{
		pdo_update('core_members',array('sign_days'=>1),array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	}
	//奖励签到积分
	$setting=uni_setting('member_setting');
	if($setting['sign']['credit']){
		cfc('core')->member_credit($_SESSION['uid'],'credit1',intval($setting['sign']['credit']),true,'member','正常签到奖励');
	}
	//返回成功状态
	exi('签到成功！','','reload');
}
if($_RQ['op']=='reward'){
	//领取签到奖励
	$post=get_postdata();
	$id=intval($post['id']);
	if(!$id){
		exi('请先选择要领的奖品！','error');
	}
	$reward=pdo_get('core_reward',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	if(!$reward){
		exi('找不到奖品！','error');
	}
	//判断是否满足领奖条件
	if($reward['type']==1){
		//连续签到奖励，领奖之后减去奖品所需连续签到次数
		$days=intval($reward['days']);
		$check=pdo_getcolumn('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'sign_days');
		if($check<$reward['days']){
			//不满足领奖条件
			exi('您的连续签到天数不足，无法领取此奖品！','error');
		}
		pdo_update('core_members',array('sign_days'=>$check-$reward['days']),array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	}else{
		//累计签到奖励，每月只能领一次。先判断当月是否领取，再判断累计签到数是否满足
		$date=mktime(0,0,0,date('m'),1,date('y'));
		$check=pdo_getcolumn('core_reward_log',array('mid'=>$_SESSION['uid'],'createtime >'=>$date,'uniacid'=>$_SESSION['uniacid']),'id');
		if($check){
			exi('您本月已领过累计签到奖励，下月再来吧！','error');
		}
		$num=pdo_count('core_qiandao',array('mid'=>$_SESSION['uid'],'createtime >'=>$date,'uniacid'=>$_SESSION['uniacid']));
		if($num<$reward['days']){
			exi('您的当月累计签到次数为'.$num.'，无法领取此奖品！看看其他奖品吧！','error');
		}
	}
	//领奖
	if($reward['credit1']){
		cfc('core')->member_credit($_SESSION['uid'],'credit1',$reward['credit1'],true,'member','连续签到奖励');
	}
	if($reward['credit2']){
		cfc('core')->member_credit($_SESSION['uid'],'credit2',$reward['credit2'],true,'member','连续签到奖励');
	}
	//插入领奖日志
	$insert=array(
		'uniacid'=>$_SESSION['uniacid'],
		'mid'=>$_SESSION['uid'],
		'rid'=>$id,
		'createtime'=>TIMESTAMP
	);
	pdo_insert('core_reward_log',$insert);
	exi('领奖成功！');
}